package net.adetech.crj.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.adetech.crj.entity.DD;
import net.adetech.crj.service.DDService;
import net.adetech.crj.utils.ServiceUtil;
import net.adetech.crj.web.IndexController;
import net.adetech.framework.entity.ServiceRequest;
import net.adetech.framework.entity.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class DemoOuterController {

    @Autowired
    ServiceUtil serviceUtil;

    @Autowired
    DDService ddService;



    @RequestMapping(value = "/demo",produces="application/json")
    public String demo(int a,int b)
    {

        ServiceRequest request = new ServiceRequest();
        Map<String,Object> params = new HashMap<String, Object>();
        params.put("a",a);
        params.put("b",b);
        request.setParams(params);
        ServiceResponse response = serviceUtil.call("demo",request);
        return JSON.toJSONString(response.getResults());
    }


    @RequestMapping(value = "/des",produces="application/json")
    public String demoDes(String input)
    {
        return decrypt(input);
    }


    private static String key = "58563214";
    private static String key1 = "32128158";

    public static String decrypt(String message) {
        try {
            System.out.println("decrypt:" + message);
//            byte[] bytesrc = new BASE64Decoder().decodeBuffer(message);
            byte[] bytesrc = Base64.getDecoder().decode(message);

            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key1.getBytes("UTF-8"));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
            byte[] retByte = cipher.doFinal(bytesrc);
            return new String(retByte,"gb2312");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping(value = "/cms",produces = "application/text")
    public String changeMaintenanceState(boolean zs,boolean ws)
    {
        IndexController.wgrMaintenance = ws;
        return "ok";
    }


    @RequestMapping(value = "/dd/ddList",produces="application/json")
    public String dd(String name)
    {


        JSONObject result = new JSONObject();
        List<DD> ddList = ddService.getDDListByDDName(name.toUpperCase());
        if (ddList!=null&&ddList.size()>0)
        {
            JSONArray valueArray = new JSONArray();
            JSONArray objectArray = new JSONArray();
            for (DD dd:ddList
                    ) {
                String key = dd.getDdKey();
                String value = dd.getDdValue();
                JSONObject object = new JSONObject();
                object.put("key",key);
                object.put("value",value);
                valueArray.add(value);
                objectArray.add(object);
            }
            result.put(name+"Array",valueArray);
            result.put(name+"ObjectArray",objectArray);
        }

        return result.toJSONString();
    }

}
